package com.ruoyi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.BrandInfo;
import com.ruoyi.domain.CategoryBrand;
import com.ruoyi.mapper.BrandInfoMapper;
import com.ruoyi.mapper.CategoryBrandMapper;
import com.ruoyi.service.IBrandInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌#brand_infoService业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@Service
public class BrandInfoServiceImpl implements IBrandInfoService
{
    @Autowired
    private BrandInfoMapper brandInfoMapper;

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;




    /**
     * 查询品牌#brand_info
     * 
     * @param brandId 品牌#brand_info主键
     * @return 品牌#brand_info
     */
    @Override
    public BrandInfo selectBrandInfoByBrandId(Long brandId)
    {
        return brandInfoMapper.selectBrandInfoByBrandId(brandId);
    }

    /**
     * 查询品牌#brand_info列表
     * 
     * @param brandInfo 品牌#brand_info
     * @return 品牌#brand_info
     */
    @Override
    public List<BrandInfo> selectBrandInfoList(BrandInfo brandInfo)
    {
        return brandInfoMapper.selectBrandInfoList(brandInfo);
    }

    /**
     * 新增品牌#brand_info
     * 
     * @param brandInfo 品牌#brand_info
     * @return 结果
     */
    @Override
    public int insertBrandInfo(BrandInfo brandInfo)
    {
        brandInfo.setCreateTime(DateUtils.getNowDate());
        return brandInfoMapper.insertBrandInfo(brandInfo);
    }

    /**
     * 修改品牌#brand_info
     * 
     * @param brandInfo 品牌#brand_info
     * @return 结果
     */
    @Override
    public int updateBrandInfo(BrandInfo brandInfo)
    {
        brandInfo.setUpdateTime(DateUtils.getNowDate());
        return brandInfoMapper.updateBrandInfo(brandInfo);
    }

    /**
     * 批量删除品牌#brand_info
     * 
     * @param brandIds 需要删除的品牌#brand_info主键
     * @return 结果
     */
    @Override
    public int deleteBrandInfoByBrandIds(Long[] brandIds)
    {
        return brandInfoMapper.deleteBrandInfoByBrandIds(brandIds);
    }

    @Override
    public List<BrandInfo> selectBrandInfoListAll() {
        List<BrandInfo> brandInfos = brandInfoMapper.selectBrandInfoListAll();


        for (BrandInfo brandInfo : brandInfos) {
            System.out.println("brandInfo. = " + brandInfo.getConeIdsStr());

            List<Integer> categoryBrands = new ArrayList<>();
            if (brandInfo.getConeIdsStr() != null) {

                String[] ss = brandInfo.getConeIdsStr().split(",");

                for (String s : ss) {
                    categoryBrands.add(Integer.parseInt(s));
                }
                // 2.将 id的list set给 品牌
            }
            brandInfo.setConeIds(categoryBrands);
        }
        return brandInfos;
    }

    @Override
    public List<BrandInfo> listByConeId(Long coneId) {
        return brandInfoMapper.selectBrandInfoListByConeId(coneId);
    }
}
